package org.jeecg.modules.bank.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.bank.entity.YcUserhomeBank;
import org.jeecg.modules.bank.entity.YcYingshengBank;
import org.jeecg.modules.bank.mapper.YcUserhomeBankMapper;
import org.jeecg.modules.bank.service.IYcUserhomeBankService;
import org.jeecg.modules.bank.service.IYcYingshengBankService;
import org.jeecg.modules.front.entity.vo.FrontUserhomeBank;
import org.jeecg.modules.front.util.YinShengUtil;
import org.jeecg.modules.fund.entity.JiStaticCount;
import org.jeecg.modules.fund.mapper.JiStaticCountMapper;
import org.jeecg.modules.gongqiang.entity.Userhome;
import org.jeecg.modules.gongqiang.entity.UserhomeAuthentication;
import org.jeecg.modules.gongqiang.entity.YcYingshengUserwallt;
import org.jeecg.modules.gongqiang.mapper.UserhomeAuthenticationMapper;
import org.jeecg.modules.gongqiang.mapper.UserhomeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description: 用户银行卡
 * @Author: jeecg-boot
 * @Date:   2024-08-25
 * @Version: V1.0
 */
@Service
public class YcUserhomeBankServiceImpl extends ServiceImpl<YcUserhomeBankMapper, YcUserhomeBank> implements IYcUserhomeBankService {
    @Autowired
    private IYcYingshengBankService ycYingshengBankService;

    @Resource
    private UserhomeAuthenticationMapper userhomeAuthenticationMapper;

    @Resource
    private UserhomeMapper userhomeMapper ;

    @Resource
    private JiStaticCountMapper jiStaticCountMapper ;





    @Override
    public Result<String> bind( FrontUserhomeBank frontUserhomeBank, Integer userhomeId)
    {
        String name=userhomeAuthenticationMapper.getByUserhomeId(userhomeId).getRealName();
        YcYingshengBank yingshengBank = ycYingshengBankService.findBank(name,frontUserhomeBank.getBankNumber());
        if(oConvertUtils.isNotEmpty(yingshengBank))
        {
            YcUserhomeBank  ycUserhomeBank=new YcUserhomeBank();
            ycUserhomeBank.setStatus(1);
            ycUserhomeBank.setType(0);
            ycUserhomeBank.setUserhomeId(userhomeId);
            ycUserhomeBank.setAccountName(name);
            ycUserhomeBank.setBankName(frontUserhomeBank.getBankName());
            ycUserhomeBank.setBankNumber(frontUserhomeBank.getBankNumber());
            ycUserhomeBank.setCreatetime(new Date());
            ycUserhomeBank.setBankTypeId(frontUserhomeBank.getBankTypeId());
            ycUserhomeBank.setVerifytime(new Date());
            ycUserhomeBank.setPhone(frontUserhomeBank.getPhone());
            ycUserhomeBank.setYingshengbankid(yingshengBank.getId());
            this.baseMapper.insert(ycUserhomeBank);
        }
        else
        {
            JiStaticCount jiCount=jiStaticCountMapper.getByFeild("create_bank");
            jiCount.setValue(jiCount.getValue()+1);
            jiStaticCountMapper.updateById(jiCount);
            Userhome userhome=userhomeMapper.selectById(userhomeId);
            String merchantno=userhome.getYingShengMerchantNo();
            Result<JSONObject> result = YinShengUtil.BindCard(merchantno,frontUserhomeBank.getBankNumber(),frontUserhomeBank.getPhone());
            if(result.isSuccess())
            {

                //银盛银行卡跨数据新增
                JSONObject yingshengResult=result.getResult();
                YcYingshengBank yingshengBankInsert=new YcYingshengBank();
                yingshengBankInsert.setAccountName(name);
                yingshengBankInsert.setMerchantno(merchantno);
                yingshengBankInsert.setBankNumber(frontUserhomeBank.getBankNumber());
                yingshengBankInsert.setCreatetime(new Date());
                yingshengBankInsert.setPhone(frontUserhomeBank.getPhone());
                yingshengBankInsert.setBankName(frontUserhomeBank.getBankName());
                yingshengBankInsert.setBankTypeId(frontUserhomeBank.getBankTypeId());
                yingshengBankInsert.setIsWallt(2);
                yingshengBankInsert.setCreatetime(new Date());
                yingshengBankInsert.setLinkid(yingshengResult.getString("linkId"));
                ycYingshengBankService.save(yingshengBankInsert);
                //用户银行卡新增新增
                YcUserhomeBank  ycUserhomeBank=new YcUserhomeBank();
                ycUserhomeBank.setStatus(1);
                ycUserhomeBank.setType(0);
                ycUserhomeBank.setUserhomeId(userhomeId);
                ycUserhomeBank.setAccountName(name);
                ycUserhomeBank.setBankName(frontUserhomeBank.getBankName());
                ycUserhomeBank.setBankNumber(frontUserhomeBank.getBankNumber());
                ycUserhomeBank.setCreatetime(new Date());
                ycUserhomeBank.setBankTypeId(frontUserhomeBank.getBankTypeId());
                ycUserhomeBank.setVerifytime(new Date());
                ycUserhomeBank.setPhone(frontUserhomeBank.getPhone());
                ycUserhomeBank.setYingshengbankid(yingshengBankInsert.getId());
                this.baseMapper.insert(ycUserhomeBank);
            }
            else
            {
                if(result.getMessage().contains("已经绑定过提现，请勿重新绑定"))
                {
                    Result<JSONObject> sresult = YinShengUtil.UserInfoByMerchantno(merchantno);
                    if(sresult.isSuccess())
                    {
                        //银盛个人钱包跨数据新增
                        YcYingshengBank yingshengBankInsert=new YcYingshengBank();
                        JSONObject yingshengResult=sresult.getResult();
                        JSONArray dataList=yingshengResult.getJSONArray("bankCardList");
                        String name2=yingshengResult.getString("name");
                        for (int i = 0; i < dataList.size(); i++) {
                            // 获取当前索引的JSON对象
                            JSONObject jsonObject = dataList.getJSONObject(i);

                            // 从JSON对象中获取键值对应的值
                            String bankName = jsonObject.getString("bankName");
                            String bankAccountNo = jsonObject.getString("bankAccountNo");
                            String linkId = jsonObject.getString("linkId");
                            String substr = frontUserhomeBank.getBankNumber().substring(0, 6);
                            bankAccountNo= frontUserhomeBank.getBankNumber().substring(0, 6);
                            if(bankName.equals(frontUserhomeBank.getBankName())&&bankAccountNo.equals(substr)&&name2.equals(name))
                            {
                                yingshengBankInsert.setLinkid(linkId);
                            }

                        }
                        yingshengBankInsert.setAccountName(name);
                        yingshengBankInsert.setMerchantno(merchantno);
                        yingshengBankInsert.setBankNumber(frontUserhomeBank.getBankNumber());
                        yingshengBankInsert.setCreatetime(new Date());
                        yingshengBankInsert.setPhone(frontUserhomeBank.getPhone());
                        yingshengBankInsert.setBankName(frontUserhomeBank.getBankName());
                        yingshengBankInsert.setBankTypeId(frontUserhomeBank.getBankTypeId());
                        yingshengBankInsert.setIsWallt(2);
                        yingshengBankInsert.setCreatetime(new Date());
                        ycYingshengBankService.save(yingshengBankInsert);
                        //用户银行卡新增新增
                        YcUserhomeBank  ycUserhomeBank=new YcUserhomeBank();
                        ycUserhomeBank.setStatus(1);
                        ycUserhomeBank.setType(0);
                        ycUserhomeBank.setUserhomeId(userhomeId);
                        ycUserhomeBank.setAccountName(name);
                        ycUserhomeBank.setBankName(frontUserhomeBank.getBankName());
                        ycUserhomeBank.setBankNumber(frontUserhomeBank.getBankNumber());
                        ycUserhomeBank.setCreatetime(new Date());
                        ycUserhomeBank.setBankTypeId(frontUserhomeBank.getBankTypeId());
                        ycUserhomeBank.setVerifytime(new Date());
                        ycUserhomeBank.setPhone(frontUserhomeBank.getPhone());
                        ycUserhomeBank.setYingshengbankid(yingshengBankInsert.getId());
                        this.baseMapper.insert(ycUserhomeBank);
                    }
                    else
                    {
                        return Result.error(result.getMessage());
                    }
                }
                else
                {
                    return Result.error(result.getMessage());
                }
            }
        }
        return Result.OK("绑定成功!");
    }

    @Override
    public Result<String> unbind(String id, Integer userhomeId)
    {
        Userhome userhome=userhomeMapper.selectById(userhomeId);
        String merchantno=userhome.getYingShengMerchantNo();
        YcUserhomeBank  ycUserhomeBank=this.baseMapper.selectById(id);
        YcYingshengBank ycYingshengBank=ycYingshengBankService.getById(ycUserhomeBank.getYingshengbankid());
        Result<JSONObject> result = YinShengUtil.Unbind(merchantno,ycYingshengBank.getLinkid());
        if(result.isSuccess())
        {
            //银盛 远程数据库银行卡置为失效
            ycYingshengBank.setStatus(0);
            ycYingshengBankService.updateById(ycYingshengBank);
            //删除银行卡
            this.baseMapper.deleteById(id);
        }
        else
        {
            return Result.error(result.getMessage());
        }
        return Result.OK("解绑定成功!");
    }

    @Override
    public IPage<YcUserhomeBank> getPageList(Page page, QueryWrapper<YcUserhomeBank> queryWrapper)
    {
        return this.baseMapper.getPageList(page,queryWrapper);
    }
}
